package com.ruoyi.quartz.task;

import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.bizsys.vo.LoanOrderVo;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.sms.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ygw
 * @Date: 2022/03/15/2:18 下午
 * @Description: 放款致歉短信定时任务类
 */
@Slf4j
@Component("loanApologizeSmsTask")
public class LoanApologizeSmsTask {

    public static final String SMS_TYEP_CNCL_RE = "temp_wdrw_cncl_re";
    public static final String SMS_TYEP_FAIL_TE = "temp_wdrw_fail_re";
    public static final String SMS_TYEP_PROC_SRY = "temp_wdrw_proc_sry";
    public static final String SMS_TYEP = "apology";
    public static final String REMARK_ACCOUNT = "Account number is invalid";
    public static final String REMARK_CANNOT = "Cannot resolve account";

    @Autowired
    private ILoanOrderService loanOrderService;

    @Autowired
    private ISmsService smsService;


    /**
     * 放款失败致歉短信
     */
    public void loanFailure() {
        log.info("开始执行loanFailure致歉短信发送任务");
        List<LoanOrderVo> loanOrderVo = loanOrderService.selectLoanOrder();
        List<SmsTask> smsTasks = new ArrayList<>();
        List<LoanOrder> loanOrders = new ArrayList<>();
        if (!loanOrderVo.isEmpty()) {
            for (int i = 0; i < loanOrderVo.size(); i++) {
                SmsTask smsTask = new SmsTask();
                LoanOrder loanOrder = new LoanOrder();
                if (StringUtils.equalsAny(loanOrderVo.get(i).getRemark(),
                        REMARK_ACCOUNT, REMARK_CANNOT)) {
                    smsTask.setSmsType(SMS_TYEP_CNCL_RE);
                    smsTask.setNoticeType(SMS_TYEP);
                    smsTask.setPhone(loanOrderVo.get(i).getPhone());
                    smsTask.setContent("Dear " + loanOrderVo.get(i).getLastName() + ", Sorry, your " + loanOrderVo.get(i).getLoanAmt() + " N withdrawal request has been canceled due to the receiving account being invalid. Please add a different account and resubmit the withdrawal application");
                    loanOrder.setSendSms(1);
                    loanOrder.setId(loanOrderVo.get(i).getId());
                } else {
                    smsTask.setSmsType(SMS_TYEP_FAIL_TE);
                    smsTask.setNoticeType(SMS_TYEP);
                    smsTask.setPhone(loanOrderVo.get(i).getPhone());
                    smsTask.setContent("Dear " + loanOrderVo.get(i).getLastName() + " , We sincerely apologize, your  " + loanOrderVo.get(i).getLoanAmt() + " N withdrawal request has been canceled due to a network issue. Please resubmit the withdrawal application. ");
                    loanOrder.setSendSms(1);
                    loanOrder.setId(loanOrderVo.get(i).getId());
                }
                //存入集合
                smsTasks.add(smsTask);
                loanOrders.add(loanOrder);
            }
            log.info("开始发送loanFailure致歉短信");
            smsService.sendBatchSms(smsTasks);


            log.info("开始修改send_sms状态");
            loanOrderService.updateBatch(loanOrders);
        }
        log.info("本次loanFailure致歉短信发送任务共：" + loanOrderVo.size() + "条");
        log.info("loanFailure致歉短信发送任务执行完毕");

    }

    /**
     * 放款等待致歉短信
     */
    public void loanPending() {
        log.info("开始执行loanPending 致歉短信发送 任务");
        //time-create_time>=30min
        List<LoanOrderVo> loanOrderVo = loanOrderService.selectLoanOrderPending();
        List<SmsTask> smsTasks = new ArrayList<>();
        List<LoanOrder> loanOrders = new ArrayList<>();
        if (!loanOrderVo.isEmpty()) {
            for (int i = 0; i < loanOrderVo.size(); i++) {
                LoanOrder loanOrder = new LoanOrder();
                SmsTask smsTask = new SmsTask();
                smsTask.setSmsType(SMS_TYEP_PROC_SRY);
                smsTask.setNoticeType(SMS_TYEP);
                smsTask.setPhone(loanOrderVo.get(i).getPhone());
                smsTask.setContent("Dear " + loanOrderVo.get(i).getLastName() + ", we sincerely apologize for the delay in your withdrawal! Your " + loanOrderVo.get(i).getLoanAmt() + " N withdrawal request will be processed within 24 hrs! If you have any questions, please contact Linkcredit WhatsApp at +2348050995364 for more!");
                loanOrder.setSendSms(1);
                loanOrder.setId(loanOrderVo.get(i).getId());
                smsTasks.add(smsTask);
                loanOrders.add(loanOrder);
            }
            log.info("开始发送loanPending致歉短信");
            smsService.sendBatchSms(smsTasks);

            log.info("开始修改send_sms状态");
            loanOrderService.updateBatch(loanOrders);
        }
        log.info("本次loanPending致歉短信发送任务共：" + loanOrderVo.size() + "条");
        log.info("loanPending致歉短信发送任务执行完毕");
    }

}
